package com.chezhujie.www.controller;


import com.chezhujie.www.domain.Czjfestivalact;
import com.chezhujie.www.domain.Czjseason;
import com.chezhujie.www.domain.Czjtop3;
import com.chezhujie.www.service.CzjseasonService;
import com.chezhujie.www.service.Czjtop3Service;
import com.xy.user.base.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.xy.core.page.Pagination;

@Controller
@RequestMapping("/top3")
public class Czjtop3Controller extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(Czjtop3Controller.class);
	public Czjtop3Controller() {
	}
 
 
 	/**
	 * czj_top3  季节新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtop3Add.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("czjtop3", new Czjtop3());
		Map<String, Object> param = new HashMap<String, Object>();
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param);
        
        List<Czjseason> czjseasons = czjtop3Service.getCzjseasonListALL(param);
         
         model.addAttribute("czjfestivalacts", czjfestivalacts);        
         model.addAttribute("czjseasons", czjseasons);
      setDict(model);
		return "top3/czjtop3Edit";
	}

	/**
	 * czj_top3  季节保存
	 * 
	 * @param czjtop3
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtop3Save.do")
	public String save(Czjtop3 czjtop3, 
			@RequestParam(value = "first", required = false)  MultipartFile first,
			HttpServletRequest request, ModelMap model) throws Exception{ 			
		try {			
			if(czjtop3.getTop3Id()!=null&&!czjtop3.getTop3Id().equals("")){
				// 编辑								
				czjtop3Service.updateCzjtop3(czjtop3);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑czj_top3  季节", "top3Id="+czjtop3.gettop3Id()));
			} else {
				// 新增
				czjtop3.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				czjtop3.setExt1("0");
				czjtop3Service.insertCzjtop3(czjtop3);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增czj_top3  季节", "top3Id="+czjtop3.gettop3Id()));
			}
   		} catch (Exception e) {
   			log.error("czjtop3 save error:", e); 
            throw e;
   		}
      
		return "redirect:czjtop3Edit.do?top3Id="+czjtop3.getTop3Id();
	}

	
	/**
	 * czj_top3  季节保存
	 * 
	 * @param czjtop3
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtop3SaveExt1.do")
	public String saveExt1(Czjtop3 czjtop3, 
			HttpServletRequest request, ModelMap model) throws Exception{    
		try {
			if(czjtop3.getTop3Id()!=null&&!czjtop3.getTop3Id().equals("")){
				// 编辑				
				czjtop3Service.updateCzjtop3(czjtop3);
				 
				String  message =null;
				
				if("1".equals(czjtop3.getExt1())){	
					message = "1";
				}
				if("0".equals(czjtop3.getExt1())){	
					message = "0";
				}				
				model.addAttribute("message",message);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑czj_top3  季节", "top3Id="+czjtop3.gettop3Id()));
			} else {
				// 新增
/*				czjtop3.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				czjtop3Service.insertCzjtop3(czjtop3);*/
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增czj_top3  季节", "top3Id="+czjtop3.gettop3Id()));
			}
   		} catch (Exception e) {
   			log.error("czjtop3 save error:", e); 
            throw e;
   		}
      
		return "redirect:czjtop3Edit.do?top3Id="+czjtop3.getTop3Id();
	}	
	
	/**
	 * czj_top3  季节删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("czjtop3Delete.do")
	public String delete(@RequestParam("ids") List<String> top3IdList) throws Exception{
		try {
         if (top3IdList != null) {
   			czjtop3Service.deleteCzjtop3(top3IdList);
   		}
         } catch (Exception e) {
   			log.error("czjtop3 delete error:", e); 
            throw e;
   		}
		return "redirect:czjtop3List.do";
	}
	 
	/**
	 * czj_top3  季节编辑页面
	 * 
	 * @param top3Id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtop3Edit.do")
	public String edit(String top3Id, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("top3Id", top3Id);
         Czjtop3 czjtop3 = czjtop3Service.getCzjtop3(param);
         
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param);
        
        List<Czjseason> czjseasons = czjtop3Service.getCzjseasonListALL(param);
         
         model.addAttribute("czjfestivalacts", czjfestivalacts);        
         model.addAttribute("czjseasons", czjseasons);
         
   		model.addAttribute("czjtop3", czjtop3);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjtop3 edit error:", e); 
            throw e;
   		}
		return "top3/czjtop3Edit";
	}

	/**
	 * czj_top3  季节详情
	 * 
	 * @param top3Id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtop3Detail.do")
	public String detail(String top3Id, HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception{
      try {
      		Map<String, Object> param = new HashMap<String, Object>();
      		param.put("top3Id", top3Id);
            Czjtop3 czjtop3 = czjtop3Service.getCzjtop3(param);
      		model.addAttribute("czjtop3", czjtop3);
            setDict(model);
            } catch (Exception e) {
   			log.error("czjtop3 detail error:", e); 
            throw e;
   		  }
		return "top3/czjtop3View";
	}
	
	
	/**
	 * czj_top3  季节详情
	 * 
	 * @param top3Id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("top3QueryImgOrder.do")
	public void detail1111(String imgOrder, String top3Id, HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception{
      try {
      		Map<String, Object> param = new HashMap<String, Object>();
      		param.put("imgOrder", imgOrder);     		    		    		
            Czjtop3 czjtop3 = czjtop3Service.queryImgOrder(param);
                          
            //插入时
      		if("".equals(top3Id)){  
      			     if(czjtop3 !=null){
      			    	jsonResult(1,"","重复的顺序",0,response);  
      			     }else{
      			    	jsonResult(1,"","可以添加",1,response);  
      			     }      				      			         
	      		}
      		//更新时      		
      		if(!"".equals(top3Id)){
          		//查询自己
          		Map<String, Object> paramMy = new HashMap<String, Object>();
          		paramMy.put("top3Id", top3Id);
                Czjtop3 myorder = czjtop3Service.getCzjtop3(paramMy);
                String myordernum = myorder.getImgOrder();
                //和自己相同的可以更新
                if(myordernum.equals(imgOrder)){
                	jsonResult(1,"","可以添加",1,response); 
                }else{
                  //如果跟自己的不相同
                	List<String> listString = czjtop3Service.getCzjtop3ALL();
                	System.out.println(listString);
                	boolean flag = true;
                	//循环数据库字段,如果相同,就代表有重复
                	for(String str : listString){
                		if(imgOrder.equals(str)){
                			flag= false;
                		}
                	}
                	System.out.println(flag);
                	if(!flag){
                		jsonResult(1,"","重复的顺序",0,response); 
                	}else{
                		jsonResult(1,"","可以添加",1,response); 
                	}
                	
                	
                }
                
                
                
      		}        		
      		
      		
      		
            } catch (Exception e) {
            jsonResult(1,"","系统异常","",response);
   			log.error("czjtop3 detail error:", e); 
            throw e;
   		  }
		
	}	
	
	 
	/**
	 * czj_top3  季节查询（分页）
	 * 
	 * @param czjtop3
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtop3List.do")
	public String listPage(Czjtop3 czjtop3, Integer pageNo, String order,
			String sort,HttpServletRequest request, ModelMap model) throws Exception{
      try {   
   		if (pageNo == null)
   			pageNo = 1;
         Pagination pagination = czjtop3Service.getCzjtop3List(toMap(czjtop3), 10, pageNo,
   				order, sort); 
         List<Czjtop3> list = (List<Czjtop3>) pagination.getList();
         for(Czjtop3 czjtop32 : list){
        	 Map<String, Object> param = new HashMap<String, Object>();
        	 param.put("festivalActId", czjtop32.getRemark());
        	 Czjfestivalact czjfestivalact = czjtop3Service.getCzjfestivalact(param);
        	 if(czjfestivalact!=null){
        		 czjtop32.setRemark(czjfestivalact.getFestName());
        	 }else{
        		 param.put("seasonId", czjtop32.getRemark());
        	   Czjseason czjseason = czjseasonService.getCzjseason(param);
        	   if(czjseason!=null){
        		   czjtop32.setRemark(czjseason.getRemark());
        	   }
        	   
        	 }
         }
         pagination.setList(list);
         
         
   		model.addAttribute("pagination", pagination);
         setDict(model);
         } catch (Exception e) {
			log.error("czjtop3 listPage error:", e); 
         throw e;
		   }
		return "top3/czjtop3List";
	}
	 
	/**
	 * czj_top3  季节查询
	 * 
	 * @param czjtop3
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtop3List1.do")
	public String list(Czjtop3 czjtop3, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String,Object> map = czjtop3Service.getCzjtop3List(toMap(czjtop3));
   		model.addAttribute("list", map.get("rows"));
   		model.addAttribute("total", map.get("total"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjtop3 list error:", e); 
            throw e;
   		}
		return "top3/czjtop3List";
	}
	 

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param czjtop3
	 * @return
	 */	private Map<String,Object> toMap(Czjtop3 czjtop3){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(czjtop3.getTop3Id()!=null&&!czjtop3.getTop3Id().equals("")){
			map.put("top3Id", czjtop3.getTop3Id());
		}
		if(czjtop3.getImgOrder()!=null&&!czjtop3.getImgOrder().equals("")){
			map.put("imgOrder", czjtop3.getImgOrder());
		}
		if(czjtop3.getImgPath()!=null&&!czjtop3.getImgPath().equals("")){
			map.put("imgPath", czjtop3.getImgPath());
		}
		if(czjtop3.getImgUrl()!=null&&!czjtop3.getImgUrl().equals("")){
			map.put("imgUrl", czjtop3.getImgUrl());
		}
		if(czjtop3.getRemark()!=null&&!czjtop3.getRemark().equals("")){
			map.put("remark", czjtop3.getRemark());
		}
		if(czjtop3.getCreatetime()!=null&&!czjtop3.getCreatetime().equals("")){
			map.put("createtime", czjtop3.getCreatetime());
		}
		if(czjtop3.getExt1()!=null&&!czjtop3.getExt1().equals("")){
			map.put("ext1", czjtop3.getExt1());
		}
		if(czjtop3.getExt2()!=null&&!czjtop3.getExt2().equals("")){
			map.put("ext2", czjtop3.getExt2());
		}
		if(czjtop3.getExt3()!=null&&!czjtop3.getExt3().equals("")){
			map.put("ext3", czjtop3.getExt3());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}
 
	//删除更新的图片
	 public void deleteFile(String jgpName){	 
		 File f = new File(jgpName); // 输入要删除的文件位置
		 if(f.exists()){
			 f.delete();
		 }
		
	 }	
     
	@Resource
	private Czjtop3Service czjtop3Service;
	
	@Resource
	private CzjseasonService czjseasonService;



}